package com.mall.controller.backend;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.common.Const;
import com.mall.common.ResponseCode;
import com.mall.common.ServerResponse;
import com.mall.pojo.MmallUser;
import com.mall.service.IOrderService;
import com.mall.service.IUserService;
import com.mall.util.CookieUtil;
import com.mall.util.JsonUtil;
import com.mall.util.RedisPoolUtil;

@Controller
@RequestMapping("/manage/order/")
public class OrderManagerController {
	
	
	@Autowired
	private IOrderService orderService;
	
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/detail.do")
	@ResponseBody
	public ServerResponse getDetail(HttpServletRequest request, Long orderNo){
		
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"NEED LOGIN");
		}
		
		if(!userService.checkAdminRole(user).isSuccess()){
			return ServerResponse.createByErrorMessage("不是管理员,无权限操作");
		}
		
		return orderService.manageGetDetail(orderNo);
	}
	
	@RequestMapping("/send_goods.do")
	@ResponseBody
	public ServerResponse send_goods(HttpServletRequest request, Long orderNo){
		
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"NEED LOGIN");
		}
		
		if(!userService.checkAdminRole(user).isSuccess()){
			return ServerResponse.createByErrorMessage("不是管理员,无权限操作");
		}
		
		return orderService.send_goods(orderNo);
	}
	
	@RequestMapping("/search.do")
	@ResponseBody
	public ServerResponse manageSearch(HttpServletRequest request, Long orderNo,@RequestParam(value="pageNum",defaultValue="1")Integer pageNum ,@RequestParam(value="pageSize",defaultValue="10")Integer pageSize ){
		
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"NEED LOGIN");
		}
		
		if(!userService.checkAdminRole(user).isSuccess()){
			return ServerResponse.createByErrorMessage("不是管理员,无权限操作");
		}
		return orderService.manageSearch(orderNo,pageNum,pageSize);
	}
	
	@RequestMapping("/list.do")
	@ResponseBody			//按订单号查询
	public ServerResponse manageList(HttpServletRequest request,@RequestParam(value="pageNum",defaultValue="1")Integer pageNum ,@RequestParam(value="pageSize",defaultValue="10")Integer pageSize ){
		
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"NEED LOGIN");
		}
		
		if(!userService.checkAdminRole(user).isSuccess()){
			return ServerResponse.createByErrorMessage("不是管理员,无权限操作");
		}
		return orderService.manageList(pageNum,pageSize);
	}
	
	
}
